﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace ParkLBS.Controller
{
    public partial class sendMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            wxmessage wx = GetWxMessage();
            string res = "";

            if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe")
            {//刚关注时的时间，用于欢迎词  
                string content = "";
                content = "你好，感谢你关注QLJ1314博客";
                res = sendTextMessage(wx, content);
            }
            else
            {
                if (wx.MsgType == "text" && wx.Content == "你好")
                {
                    res = sendTextMessage(wx, "你好，感谢你关注QLJ1314博客!");
                }
                else
                {
                    res = sendTextMessage(wx, "这个我也没遇见过，正在向微信客服反应此事，请耐心等待或者可以直接打10086!");
                }
            }

            Response.Write(res);
        }
        //获取用户基本信息
        private wxmessage GetWxMessage()
        {
            wxmessage wx = new wxmessage();
            StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8);
            XmlDocument xml = new XmlDocument();
            xml.Load(str);
            wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
            wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
            wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
            if (wx.MsgType.Trim() == "text")
            {
                wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText;
            }
            if (wx.MsgType.Trim() == "event")
            {
                wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
            }


            return wx;
        }

        /// <summary>    
        /// 发送文字消息    
        /// </summary>    
        /// <param name="wxCont">获取的收发者信息    
        /// <param name="content">内容    
        /// <returns>string </returns>    
        private string sendTextMessage(wxmessage wxCont, string content)
        {
            string res = string.Format(ReplyType.Message_Text,
                wxCont.FromUserName, wxCont.ToUserName, DateTime.Now, content);
            return res;
        }
    }
}